package cc.eddic.practice.concurrency;

import cc.eddic.practice.concurrency.impl.CDNLogItemParserCxyImpl;
import cc.eddic.practice.concurrency.impl.CDNLogParserWithLoopCxyImpl;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

@Slf4j
@SpringBootApplication
public class ConcurrencyCxyApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(ConcurrencyCxyApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {

//        long start = System.currentTimeMillis();
//        log.info("start");
//        loop();
//        executor();
//        log.info("elapsed {} ms", System.currentTimeMillis()-start);

        long start = System.currentTimeMillis();
        if (log.isInfoEnabled()) log.info("Hello World!");
        val folder = "C:\\Users\\18235\\Desktop\\2023-04\\2023-04";
//        val folder = "C:\\Users\\18235\\Documents\\WeChat Files\\wxid_kenddyh0u82912\\FileStorage\\File\\2023-08\\nginx-logs\\nginx-logs";

//        val logItems = logParser.parse(folder);
        val logItems = cdnLogParser.parse(folder);
//        val logItems = parser.parse(folder);
        if (log.isInfoEnabled()) {

            log.info("elapsed {}ms", System.currentTimeMillis() - start);
            log.info("files : {}", logItems.size());
            log.info("total log items : {}", logItems.values().stream().mapToLong(Collection::size).sum());
//            log.info("valid log items : {}", logItems.values().stream().flatMap(Collection::stream).filter(logItems::isValidRequest).count());

//      输出所有log
//            logItems.forEach((filePath, logItemList) -> {
//                List<LogItem> invalidLogItems = logItemList.stream()
//                        .filter(logItem -> !logItem.isValidRequest())
//                        .collect(Collectors.toList());
//
//                if (!invalidLogItems.isEmpty()) {
//                    StringBuilder invalidLogMessage = new StringBuilder();
//                    invalidLogMessage.append("Invalid log items in file ").append(filePath).append(":").append(System.lineSeparator());
//
//                    for (LogItem invalidLogItem : invalidLogItems) {
//                        invalidLogMessage.append(invalidLogItem).append(System.lineSeparator());
//                    }
//
//                    log.info(invalidLogMessage.toString());
//                }
//            });


        }

//    private void loop() {
//        for (int i = 0; i< LOG_FILE_COUNT; i++){
//            dummyParse(i);
//    }
//}

//    private void executor() {
//        ExecutorService executorService = Executors.newFixedThreadPool(384);
//        CountDownLatch latch = new CountDownLatch(LOG_FILE_COUNT);
//        for (int i=0; i<LOG_FILE_COUNT; i++){
//            int finalI = i;
//            executorService.submit(() -> dummyParse(finalI, latch));
//        }
//        try {
//            latch.await();
//        } catch (InterruptedException e){
//            log.error(e.getMessage());
//        }
//
//        ExecutorServiceUtil.shutdown(executorService);
//
//    }

//    private void dummyParse(int i, CountDownLatch latch){
//        dummyParse(i);
//        latch.countDown();
//    }
//
//
//    private void dummyParse(int i){
//        try {
////            val r = rnd(10, 50);
//            val r = 100;
//            if (log.isInfoEnabled()) log.info("{} => sleep {} ms", i, r);
//            Thread.sleep(r);
//        } catch (InterruptedException e) {
//            log.error(e.getMessage());
//        }
//    }
//
//    private final int LOG_FILE_COUNT = 256;
//
//
//    private int rnd(int min, int max) {
//        return random.nextInt(max - min +1 ) + min;
    }

//    private final Random random = new Random(System.currentTimeMillis());

    //        public ConcurrencyCxyApplication(@Qualifier("LogParserWithLoopCxyImpl") LogParser logParser) {
//        public ConcurrencyCxyApplication(@Qualifier("LogParserWithParallelCxyImpl") LogParser logParser) {
//    public ConcurrencyCxyApplication(@Qualifier("LogItemParserWithFutureCxyImpl2") LogParser logParser) {
    public ConcurrencyCxyApplication(@Qualifier("LogParserWithLoopCxyImpl") LogParser logParser,
                                     @Qualifier("CDNLogParserWithLoopCxyImpl") CDNLogParser cdnLogParser) {
        this.logParser = logParser;
        this.cdnLogParser = cdnLogParser;
    }

    private final LogParser logParser;
    private final CDNLogParser cdnLogParser;



}
